import React, { Component } from 'react'
import PubSub from 'pubsub-js'
import Far from './Far'

export default class App extends Component {
  componentDidMount() {
    // id是当前订阅的id.类似于定时器的id
    this.id = PubSub.subscribe('richWomen', (topic, data) => {
      console.log('app接收到富婆话题了', topic, data)
    })
    this.id1 = PubSub.subscribe('prettyWomen', (topic, data) => {
      console.log('app接收到白富美话题了', topic, data)
    })
  }

  componentWillUnmount() {
    PubSub.unsubscribe(this.id)
    PubSub.unsubscribe(this.id1)
  }
  render() {
    return (
      <div>
        app
        <hr />
        <button
          onClick={() => {
            PubSub.unsubscribe(this.id)
          }}
        >
          清除指定订阅(app的富婆话题)
        </button>
        <button
          onClick={() => {
            PubSub.unsubscribe('richWomen')
          }}
        >
          清除所有富婆的订阅
        </button>
        <button
          onClick={() => {
            PubSub.clearAllSubscriptions()
          }}
        >
          清除所有订阅
        </button>
        <hr />
        <Far></Far>
      </div>
    )
  }
}
